import until from './until.js'
let api = {
	// version: plus.runtime.version,
	// platform: uni.getSystemInfoSync().osName, //客户端平台，值域为：ios、android
	scanCode: function() {
		return new Promise((resolve, reject) => {
			uni.scanCode({
				onlyFromCamera: true,
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				}
			})
		})
	},
	//加密
	post: function(url, data) {
		uni.getNetworkType({
			success: function(res) {
				if (res.networkType == 'none') {
					uni.showModal({
						content: '网路不畅请检查您的网路是否连接',
						showCancel: false
					})
					return
				}
			}
		});
		data.version = this.version;
		data.platform = this.platform;
		let baseUrl = getApp().globalData.baseUrl;
		let environment = uni.getStorageSync('environment');
		if (environment == 2) {
			baseUrl = getApp().globalData.developUrl;
		}
		var website = baseUrl + url;
		data.timestamp = Date.parse(new Date());
		data.token = uni.getStorageSync('token');
		data.user_id = uni.getStorageSync('user_id');
		data.signdata = until.sign(data); //加密
		delete data.token;
		uni.showLoading({
			title: '加载中...'
		})
		return new Promise((resolve, reject) => {
			uni.request({
				url: website,
				data,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				},
				complete: function(res) {
					uni.hideLoading()
				}
			})
		})
	},
	//加密
	get: function(url, data) {
		uni.getNetworkType({
			success: function(res) {
				if (res.networkType == 'none') {
					uni.showModal({
						content: '网路不畅请检查您的网路是否连接',
						showCancel: false
					})
					return
				}
			}
		});
		data.version = this.version;
		data.platform = this.platform;
		let baseUrl = getApp().globalData.baseUrl;
		let environment = uni.getStorageSync('environment');
		if (environment == 2) {
			baseUrl = getApp().globalData.developUrl;
		}
		var website = baseUrl + url;
		data.timestamp = Date.parse(new Date());
		data.token = uni.getStorageSync('token');
		data.user_id = uni.getStorageSync('user_id');
		data.signdata = until.sign(data); //加密
		delete data.token;
		uni.showLoading({
			title: '加载中...'
		})
		return new Promise((resolve, reject) => {
			uni.request({
				url: website,
				data,
				method: 'GET',
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				},
				complete: function(res) {
					uni.hideLoading()
				}
			})
		})
	},
	postNoSign: function(url, data) {
		uni.getNetworkType({
			success: function(res) {
				if (res.networkType == 'none') {
					uni.showModal({
						content: '网路不畅请检查您的网路是否连接',
						showCancel: false
					})
					return
				}
			}
		});
		data.version = this.version;
		data.platform = this.platform;
		let baseUrl = getApp().globalData.baseUrl;
		let environment = uni.getStorageSync('environment');
		if (environment == 2) {
			baseUrl = getApp().globalData.developUrl;
		}
		var website = baseUrl + url;
		uni.showLoading({
			title: '加载中...'
		})
		return new Promise((resolve, reject) => {
			uni.request({
				url: website,
				data,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				},
				complete: function(res) {
					uni.hideLoading()
				}
			})
		})
	},
	getNoSign: function(url, data) {
		uni.getNetworkType({
			success: function(res) {
				if (res.networkType == 'none') {
					uni.showModal({
						content: '网路不畅请检查您的网路是否连接',
						showCancel: false
					})
					return
				}
			}
		});
		// data.version = this.version;
		// data.platform = this.platform;
		let baseUrl = getApp().globalData.baseUrl;
		// let environment = uni.getStorageSync('environment');
		// if (environment == 2) {
		// 	baseUrl = getApp().globalData.developUrl;
		// }
		var website = baseUrl + url;
		uni.showLoading({
			title: '加载中...'
		})
		return new Promise((resolve, reject) => {
			uni.request({
				url: website,
				data,
				method: 'GET',
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				},
				complete: function(res) {
					uni.hideLoading()
				}
			})
		})
	},
	upload: function(url, data, name, filePath) {
		uni.getNetworkType({
			success: function(res) {
				if (res.networkType == 'none') {
					uni.showModal({
						content: '网路不畅请检查您的网路是否连接',
						showCancel: false
					})
					return
				}
			}
		});
		data.version = this.version;
		data.platform = this.platform;
		let baseUrl = getApp().globalData.baseUrl;
		let environment = uni.getStorageSync('environment');
		if (environment == 2) {
			baseUrl = getApp().globalData.developUrl;
		}
		var website = baseUrl + url;
		data.timestamp = Date.parse(new Date());
		data.token = uni.getStorageSync('token');
		data.user_id = uni.getStorageSync('user_id');
		data.signdata = until.sign(data); //加密
		delete data.token;
		uni.showLoading({
			title: '上传中...'
		})
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: website,
				filePath: filePath,
				name: name,
				formData: data,
				success: function(res) {
					resolve(res)
				},
				fail: function(error) {
					reject(error)
				}
			})
			uploadTask.onProgressUpdate((res) => {
				if (res.progress == 100) {
					uni.hideLoading();
				}
			});
		})
	}
}

export default api
